package lan.core.file

import java.io._
import lan.core.io._

class FileEntry(val file: File) extends IOAware with FileRepresentation {

  var keys: List[String] = Nil
  
  var description: String = file.getName
  
  val fileName = file.getName.toLowerCase
  
  lazy val md5 = calcMd5
  
  private def calcMd5 = {
    import java.security._
    val md = MessageDigest.getInstance("MD5")
    eachChunk(new FileInputStream(file), new Array[Byte](10240)) {
      md.update(_, 0, _)
    }
    md.digest.map(Integer.toHexString(_)).mkString("", "", "")
  }
  
  override def hashCode: Int = file.length.asInstanceOf[Int]
  
  override def equals(a: Any) = {
    a != null && a.isInstanceOf[FileEntry] && hashCode == a.hashCode && md5 == a.asInstanceOf[FileEntry].md5
  }
  
  override def toString = fileName
  
  def name = fileName

  def keywords = keys

}
